# cmd: python3 helper4VersionInfoSync4And.py
# desc: 同步../pubspec.yaml版本信息到../android/guang_build.properties
import os
import json
import string
import sys
import time
import getopt

try:
    import jproperties
except ImportError:
    os.system("pip3 install jproperties")
    import jproperties
else:
    pass


import helperUtils as Utils

SCRIPT_DIR = os.path.dirname(__file__)
SCRIPT_NAME = os.path.basename(__file__)
_TAG = SCRIPT_NAME


def syncVersionInfo():
    with open(SCRIPT_DIR + '/../gradle.properties', 'r') as gradle_file:
        # try:
            gd = jproperties.Properties()
            gd.load(gradle_file)
            # Utils.log(_TAG, parsed_yaml)
            versionCode = gd["VERSION_CODE"]
            versionNme = gd["VERSION_NAME"]
        
            Utils.log(_TAG, "-----------> VersionCode in pubspec.yaml:" + versionCode)
            Utils.log(_TAG, "-----------> VersionName in pubspec.yaml:" + versionNme)
            and_guang_build_cfg_file = SCRIPT_DIR + '/../guang_build.properties'
            if os.path.exists(and_guang_build_cfg_file):
                print('file already exists')
            else:
                # create a file
                with open(and_guang_build_cfg_file, 'w') as fp:
                    # uncomment if you want empty file
                    fp.write('# create')
            configs = jproperties.Properties()
            with open(and_guang_build_cfg_file, 'rb') as config_file:
                configs.load(config_file)
            # Utils.log(_TAG, "VersionCode in guang_build.properties:" + configs.properties["GooglePlay.VersionCode"])
            # Utils.log(_TAG, "VersionName in guang_build.properties:" + configs.properties["GooglePlay.VersionName"])
            configs.properties['GooglePlay.VersionCode'] = versionCode
            configs.properties['GooglePlay.VersionName'] = versionNme
            # 判断是否是要上传到GooglePlay，通过相关文件是否存在判断
            GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES = SCRIPT_DIR + "/../GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES.tmp"
            if os.path.exists(GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES):
                with open(GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES, 'rt') as gp_version_file:
                    try:
                        gp_version_file_txt = gp_version_file.read()
                        feature_txt1 = "Result: ["
                        lst_version_bgn_idx = gp_version_file_txt.find(feature_txt1)
                        lst_version_bgn_idx += len(feature_txt1)
                        gp_version_file_txt = gp_version_file_txt[lst_version_bgn_idx:]
                        feature_txt2 = ","
                        lst_version_end_idx = gp_version_file_txt.index(feature_txt2)
                        gp_version = gp_version_file_txt[0:lst_version_end_idx]
                        gp_version_int = int(gp_version)
                        gp_version_int_next = gp_version_int + 1
                        Utils.log(_TAG,
                                  "curr google play internal track version:%s ,increase it to:%s" % (
                                  gp_version_int, gp_version_int_next))
                        version_cod_in_yaml_int = int(versionCode)
                        if version_cod_in_yaml_int < gp_version_int_next:
                            # yaml 中配置的版本号小于google play上的版本号时，自动取正确的版本号
                            configs.properties['GooglePlay.VersionCode'] = gp_version_int_next
                    except:
                        pass
                pass
            with open(SCRIPT_DIR + '/../guang_build.properties', 'wb') as config_file:
                configs.store(config_file)
    pass

syncVersionInfo()
